//
//  GQHInvoiceView.m
//  Xanthium
//
//  Created by GuanQinghao on 28/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHInvoiceView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"


@interface GQHInvoiceView() <UITableViewDelegate,UITableViewDataSource, UITextFieldDelegate>
{
    
    NSArray *titleArray;
    NSArray *_placeHolderArray;
}

@end


@implementation GQHInvoiceView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"购买方名称",@"纳税人识别号",@"收件地址",@"收件电话",@"开户账号",@"开户行",@"应税劳务名称",@"规格型号",@"数量",@"合计税价",@"备注"];
    if (!_qh_contentArray) {
        
        _qh_contentArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]];
        _placeHolderArray = @[@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入",@"请输入"];
    }
}

- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    
    [self.qh_tableView reloadData];
}

- (void)setQh_orderModel:(GQHOrderModel *)qh_orderModel
{
    _qh_orderModel = qh_orderModel;
    
    [_qh_contentArray replaceObjectAtIndex:8 withObject:[NSString stringWithFormat:@"%.1f", qh_orderModel.qh_freightWeight]];
    
    [_qh_contentArray replaceObjectAtIndex:9 withObject:qh_orderModel.qh_amount];
    
    [self.qh_tableView reloadData];
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIView *orderNumberView = [[UIView alloc] init];
    orderNumberView.backgroundColor = kAppRedColor;
    [self addSubview:orderNumberView];
    [orderNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
    
    _qh_orderNumberLabel = [[UILabel alloc] init];
    _qh_orderNumberLabel.font = kSmallerFont;
    _qh_orderNumberLabel.textColor = kWhiteColor;
    [orderNumberView addSubview:_qh_orderNumberLabel];
    [_qh_orderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(orderNumberView);
        make.left.and.right.mas_equalTo(orderNumberView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight + 30);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_contentArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.enterText.text = _qh_contentArray[indexPath.row];
    cell.enterText.placeholder = _placeHolderArray[indexPath.row];
    cell.enterText.tag = indexPath.row;
    cell.enterText.delegate = self;
    cell.isEnter = YES;
    if (!self.isEdit)
    {
        cell.enterText.enabled = NO;
    }
    
    if (indexPath.row == 8)
    {
        cell.perLabel.text = @"吨";
    }
    else if (indexPath.row == 9)
    {
        cell.enterText.keyboardType = UIKeyboardTypeDecimalPad;
        cell.perLabel_width.constant = 0;
        cell.enterText.enabled = NO;
    }
    else
    {
        cell.perLabel_width.constant = 0;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
//
//        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}

@end
